<?php

/**
 * Planificacion form.
 *
 * @package    sistema_estadisticas
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class PlanificacionForm extends BasePlanificacionForm
{

	public function configure()
	{
		$form = new sfForm();

		$this->widgetSchema['docente_id'] = new WidgetFormAutocomplete(
			array(
				'model' => 'Docente',
				'action' => 'docente/buscar'
			)
		);

		$this->widgetSchema['modulo_id'] = new sfWidgetFormChoice(array('choices' => array()));

		if ($this->isNew())
		{
			$this->widgetSchema['carrera_id']->setOption('add_empty', true);
		}
		else
		{
			$this->widgetSchema['docente_id']->setOption('values', array($this->getObject()->getDocente()));
			$horarios = HorarioClaseTable::getInstance()->findAll();
			$choices = array();

			foreach ($horarios as $key => $value)
				$choices[$value->getId()] = $value->__toString();

			$cargas = $this->getObject()->getCargaHoraria();

			$i = 0;
			foreach ($cargas as $carga)
			{
				$cargaForm =  new CargaHorariaForm($carga);
				$cargaForm->widgetSchema['horario_id'] = new sfWidgetFormChoice(array('choices' => $choices));
				$form->embedForm('carga_' . $i++, $cargaForm);
			}

			$modulos = ModuloTable::getInstance()->findByCarreraId($this->getObject()->carrera_id);
			$choices = array();
			foreach ($modulos as $key => $value)
				$choices[$value->id] = $value->__toString();

			$this->widgetSchema['modulo_id']->setOption('choices', $choices);
		}

		$this->embedForm('carga_horaria', $form);
	}

	public function embedCargaForm($index)
	{
		$form = $this->getEmbeddedForm('carga_horaria');
		$form->embedForm('carga_' . $index, new CargaHorariaForm());
		$this->embedForm('carga_horaria', $form);

		return $this['carga_horaria']['carga_' . $index];
	}
	
	public function bind(array $taintedValues = null, array $taintedFiles = null)
	{
		$holder = $this->getEmbeddedForm('carga_horaria');
		
		//cargas eliminadas
		foreach ($holder as $key => $form)
		{
			if (!isset($taintedValues['carga_horaria'][$key]))
			{
				$holder->widgetSchema->offsetUnset($key);
				$holder->validatorSchema[$key] = new sfValidatorPass();
			}
		}
		
		//cargas agregadas
		if (isset($taintedValues['carga_horaria']))
		{
			foreach ($taintedValues['carga_horaria'] as $key => $values)
			{
				$index = substr($key, 6);
				if (!isset($holder[$key]))
					$holder->embedForm($key, new CargaHorariaForm());
			}
		}
		
		$this->embedForm('carga_horaria', $holder);
		
		parent::bind($taintedValues, $taintedFiles);
	}
	
	protected function doSave($con = null)
	{
		$this->updateObject();
		
		$this->getObject()->save($con);
		
		$holder = $this->getEmbeddedForm('carga_horaria');
		
		foreach ($holder->getEmbeddedForms() as $key => $form)
		{
			if ($form->isNew())
				$form->getObject()->setPlanificacionId($this->getObject()->getId());
			else
			{
				if (!isset($this->taintedValues['carga_horaria'][$key]))
				{
					$holder->offsetUnset($key);
					$form->getObject()->delete($con);
				}
			}
		}
		
		$this->saveEmbeddedForms($con);
	}

	public function getJavascripts()
	{
		return array_merge(parent::getJavascripts(), array('planificacion.js'));
	}

}
